package common

import (
	"context"
	"errors"
	"git.mycaigou.com/gfyx/common/authorize"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/common"
	repo "git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/repository/common"
	pb "git.mycaigou.com/gfyx/micro-gfyx-api/proto/micro_gfyx_user_service_proto/micro-gfyx-user-service"
	"git.myscrm.cn/golang/stark/v4"
)

type SiteAnnouncementService struct {
	siteAnnouncementRepository repo.SiteAnnouncementRepositoryIface
}

func NewSiteAnnouncementService(siteAnnouncementRepository repo.SiteAnnouncementRepositoryIface) SiteAnnouncementServiceIface {
	return &SiteAnnouncementService{siteAnnouncementRepository: siteAnnouncementRepository}
}

func (s *SiteAnnouncementService) SaveSiteAnnouncement(ctx context.Context, request *common.SiteAnnouncementSaveRequest) (*common.SiteAnnouncementSaveResponse, error) {
	loginUserName := authorize.GetLoginZhanTuUserName(ctx)
	if loginUserName == "" {
		return nil, errors.New("用户未登录")
	}
	params := &pb.SaveSiteAnnouncementRequest{
		Title:     request.Title,
		Content:   request.Content,
		Url:       request.Url,
		Cycle:     request.Cycle,
		StartTime: request.StartTime,
		EndTime:   request.EndTime,
		CreatedBy: loginUserName,
	}
	if request.Id > 0 {
		params.Id = request.Id
	}
	response, err := s.siteAnnouncementRepository.SaveSiteAnnouncement(ctx, params)
	if err != nil {
		stark.Logger.Errorf(ctx, "SaveSiteAnnouncement failed,params:%v, err:%v", params, err)
		return nil, err
	}
	return &common.SiteAnnouncementSaveResponse{
		Id: response.Id,
	}, nil
}

func (s *SiteAnnouncementService) GetSiteAnnouncementManageList(ctx context.Context, request *common.SiteAnnouncementManageListRequest) (*common.SiteAnnouncementManageListResponse, error) {
	params := &pb.GetSiteAnnouncementListRequest{
		Page:     request.Page,
		PageSize: request.PageSize,
	}
	result, err := s.siteAnnouncementRepository.GetSiteAnnouncementManageList(ctx, params)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetSiteAnnouncementManageList failed,params:%v, err:%v", params, err)
		return nil, err
	}
	response := &common.SiteAnnouncementManageListResponse{
		Total: result.Total,
		List:  make([]*common.SiteAnnouncementManageItem, 0),
	}
	for _, item := range result.List {
		response.List = append(response.List, &common.SiteAnnouncementManageItem{
			Id:        item.Id,
			Title:     item.Title,
			Content:   item.Content,
			Url:       item.Url,
			Cycle:     item.Cycle,
			StartTime: item.StartTime,
			EndTime:   item.EndTime,
			CreatedOn: item.CreatedOn,
			CreatedBy: item.CreatedBy,
		})
	}
	return response, nil
}

func (s *SiteAnnouncementService) GetSiteAnnouncementDetail(ctx context.Context, request *common.SiteAnnouncementDetailRequest) (*common.SiteAnnouncementDetailResponse, error) {
	params := &pb.GetSiteAnnouncementDetailRequest{
		Id: request.Id,
	}
	detail, err := s.siteAnnouncementRepository.GetSiteAnnouncementDetail(ctx, params)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetSiteAnnouncementDetail failed,params:%v, err:%v", params, err)
		return nil, err
	}
	return &common.SiteAnnouncementDetailResponse{
		Id:        detail.Id,
		Title:     detail.Title,
		Content:   detail.Content,
		Url:       detail.Url,
		Cycle:     detail.Cycle,
		StartTime: detail.StartTime,
		EndTime:   detail.EndTime,
		CreatedBy: detail.CreatedBy,
		CreatedOn: detail.CreatedOn,
	}, nil
}

func (s *SiteAnnouncementService) DeleteSiteAnnouncement(ctx context.Context, request *common.SiteAnnouncementDeleteRequest) (*common.SiteAnnouncementDeleteResponse, error) {
	loginUserName := authorize.GetLoginZhanTuUserName(ctx)
	if loginUserName == "" {
		return nil, errors.New("用户未登录")
	}
	params := &pb.DeleteSiteAnnouncementRequest{
		Id:         request.Id,
		ModifiedBy: loginUserName,
	}
	_, err := s.siteAnnouncementRepository.DeleteSiteAnnouncement(ctx, params)
	if err != nil {
		stark.Logger.Errorf(ctx, "DeleteSiteAnnouncement failed,params:%v, err:%v", params, err)
		return nil, err
	}
	return &common.SiteAnnouncementDeleteResponse{}, nil
}

func (s *SiteAnnouncementService) GetUserSiteAnnouncementList(ctx context.Context, request *common.GetUserSiteAnnouncementListRequest) (*common.GetUserSiteAnnouncementListResponse, error) {
	if request.Page == 0 && request.Page > 1000 {
		request.Page = 1
	}
	if request.PageSize == 0 && request.PageSize > 500 {
		request.PageSize = 20
	}
	params := &pb.GetUserSiteAnnouncementListRequest{
		Page:       request.Page,
		PageSize:   request.PageSize,
		UserId:     authorize.GetUserId(ctx),
		TenantCode: authorize.GetTenantCode(ctx),
	}
	result, err := s.siteAnnouncementRepository.GetUserSiteAnnouncementList(ctx, params)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetUserSiteAnnouncementList failed,params:%v, err:%v", params, err)
		return nil, err
	}
	response := &common.GetUserSiteAnnouncementListResponse{
		Total: result.Total,
		List:  make([]*common.SiteAnnouncementManageItem, 0),
	}

	for _, item := range result.List {
		response.List = append(response.List, &common.SiteAnnouncementManageItem{
			Id:        item.Id,
			Title:     item.Title,
			Content:   item.Content,
			Url:       item.Url,
			Cycle:     item.Cycle,
			StartTime: item.StartTime,
			EndTime:   item.EndTime,
			CreatedOn: item.CreatedOn,
			CreatedBy: item.CreatedBy,
		})
	}
	return response, nil
}

func (s *SiteAnnouncementService) SetUserSiteAnnouncementRead(ctx context.Context, request *common.SetSiteAnnouncementReadRequest) (*common.SetSiteAnnouncementReadResponse, error) {
	// 获取登录用户
	params := &pb.SetSiteAnnouncementReadRequest{
		SiteAnnouncementId: request.Id,
		UserId:             authorize.GetUserId(ctx),
		TenantCode:         authorize.GetTenantCode(ctx),
	}
	result, err := s.siteAnnouncementRepository.SetUserSiteAnnouncementRead(ctx, params)
	if err != nil {
		stark.Logger.Errorf(ctx, "SetUserSiteAnnouncementRead failed,params:%v, err:%v", params, err)
		return nil, err
	}
	return &common.SetSiteAnnouncementReadResponse{
		Id: result.Id,
	}, nil
}
